<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/12/31
 * Time: 14:48
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\BoxOwnerModel;
use App\Http\Model\Mysql\BoxSystemModel;
use App\MultiTrait\PassportTrait;
use Illuminate\Support\Facades\Hash;

class SystemService extends BaseService
{
    use PassportTrait;

    /**
     * 盒子用户登录
     * @param string $account
     * @param string $password
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function ownerLogin(string $account, string $password)
    {
        $owner_info =   BoxOwnerModel::where(['phone' => $account])->select(['id', 'phone', 'nick_name', 'password'])->first();
        if (empty($owner_info)) {
            return [false, '账号不存在'];
        }

        if (!Hash::check($password, $owner_info->password)) {
            return [false, '密码错误'];
        }

        $login_result   =   $this->authenticate('box_owner', [
            'grant_type'    =>  'client_credentials',
            'client_id'     =>  '7',
            'client_secret' =>  '1IjeTmDi4dKf5DGfUQbKo4SRsdnNTKMWAppHXhUa',
            'username'      =>  $account,
            'password'      =>  $password,
            'scope'         =>  '',
            'guard_provider'=>  'box_owner'
        ]);

        unset($owner_info->password);
        return [true, array_merge($login_result, $owner_info->toArray())];
    }

    /**
     * 盒子管理员登录
     * @param string $account
     * @param string $password
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function systemLogin(string $account, string $password)
    {
        $system_info    =   BoxSystemModel::where(['account' => $account])->select(['id', 'account', 'nick_name', 'password'])->first();
        if (empty($system_info)) {
            return [false, '账号不存在'];
        }

        if (!Hash::check($password, $system_info->password)) {
            return [false, '密码错误'];
        }

        $login_result   =   $this->authenticate('box_system', [
            'grant_type'    =>  'client_credentials',
            'client_id'     =>  '6',
            'client_secret' =>  'hh9pPQ7jnegmW9jZy32bCDUaTjQPx4OPw0oymmUA',
            'username'      =>  $account,
            'password'      =>  $password,
            'scope'         =>  '',
            'guard_provider'=>  'box_system'
        ]);

        unset($system_info->password);
        return [true, array_merge($login_result, $system_info->toArray())];
    }

}
